package org.jeecg.modules.demo.decoration.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.decoration.entity.DecorationDeposit;
import org.jeecg.modules.demo.decoration.entity.DecorationDepositTotal;
import org.jeecg.modules.demo.decoration.entity.Yajin;
import org.jeecg.modules.demo.decoration.mapper.YajinMapper;
import org.jeecg.modules.demo.decoration.service.IDecorationDepositService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 装修保证金
 * @Author: jeecg-boot
 * @Date:   2024-03-29
 * @Version: V1.0
 */
@Api(tags="装修保证金")
@RestController
@RequestMapping("/decoration/decorationDeposit")
@Slf4j
public class DecorationDepositController extends JeecgController<DecorationDeposit, IDecorationDepositService> {
	@Autowired
	private IDecorationDepositService decorationDepositService;

	/**
	 * 分页列表查询
	 *
	 * @param decorationDeposit
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "装修保证金-分页列表查询")
	@ApiOperation(value="装修保证金-分页列表查询", notes="装修保证金-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<DecorationDepositTotal>> queryPageList(DecorationDeposit decorationDeposit,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<DecorationDeposit> queryWrapper = QueryGenerator.initQueryWrapper(decorationDeposit, req.getParameterMap());
		Page<DecorationDeposit> page = new Page<DecorationDeposit>(pageNo, pageSize);
		IPage<DecorationDeposit> pageList = decorationDepositService.page(page, queryWrapper);
		DecorationDepositTotal decorationDepositTotal = new DecorationDepositTotal();
		decorationDepositTotal.setDepositCollection(0.0);
		decorationDepositTotal.setTuibaozhengjin(0.0);
		//计算合计
		for (DecorationDeposit deposit : decorationDepositService.list(queryWrapper)) {
			if (deposit.getDepositCollection() != null){
				decorationDepositTotal.setDepositCollection(decorationDepositTotal.getDepositCollection()+deposit.getDepositCollection());
			}
			if (deposit.getTuibaozhengjin() != null){
				decorationDepositTotal.setTuibaozhengjin(decorationDepositTotal.getTuibaozhengjin()+deposit.getTuibaozhengjin());
			}
		}
		BeanUtils.copyProperties(pageList,decorationDepositTotal);
		return Result.OK(decorationDepositTotal);
	}

	@Autowired
	private YajinMapper yajinMapper1;

	@ApiOperation(value="装修保证金-分页列表查询", notes="装修保证金-分页列表查询")
	@GetMapping(value = "/list2")
	public Result<List<Yajin>> queryPageList2(DecorationDeposit decorationDeposit,
											  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
											  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
											  HttpServletRequest req) {
		List<Yajin> yajins = yajinMapper1.selectList(null);
		return Result.OK(yajins);
	}

	/**
	 *   添加
	 *
	 * @param decorationDeposit
	 * @return
	 */
	@AutoLog(value = "装修保证金-添加")
	@ApiOperation(value="装修保证金-添加", notes="装修保证金-添加")
	@RequiresPermissions("decoration:decoration_deposit:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody DecorationDeposit decorationDeposit) {
		decorationDepositService.save(decorationDeposit);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param decorationDeposit
	 * @return
	 */
	@AutoLog(value = "装修保证金-编辑")
	@ApiOperation(value="装修保证金-编辑", notes="装修保证金-编辑")
	@RequiresPermissions("decoration:decoration_deposit:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody DecorationDeposit decorationDeposit) {
		decorationDepositService.updateById(decorationDeposit);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "装修保证金-通过id删除")
	@ApiOperation(value="装修保证金-通过id删除", notes="装修保证金-通过id删除")
	@RequiresPermissions("decoration:decoration_deposit:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		decorationDepositService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "装修保证金-批量删除")
	@ApiOperation(value="装修保证金-批量删除", notes="装修保证金-批量删除")
	@RequiresPermissions("decoration:decoration_deposit:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.decorationDepositService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "装修保证金-通过id查询")
	@ApiOperation(value="装修保证金-通过id查询", notes="装修保证金-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<DecorationDeposit> queryById(@RequestParam(name="id",required=true) String id) {
		DecorationDeposit decorationDeposit = decorationDepositService.getById(id);
		if(decorationDeposit==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(decorationDeposit);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param decorationDeposit
    */
    @RequiresPermissions("decoration:decoration_deposit:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, DecorationDeposit decorationDeposit) {
        return super.exportXls(request, decorationDeposit, DecorationDeposit.class, "装修保证金");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("decoration:decoration_deposit:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, DecorationDeposit.class);
    }

}
